﻿using Microsoft.AspNetCore.Mvc;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using SmartConference.Api.Applocations.Services;
using SmartConference.Infrastructure.Interfaces;
using System.Data;

namespace SmartConference.Api.Filter
{
    public class ExportHelper<T>
    {
        private readonly IBaseRepository<T> baseRepository;

        public ExportHelper(IBaseRepository<T> baseRepository)
        {
            this.baseRepository = baseRepository;
        }
        public byte[] Export<T>(string rowName,string titleName)
        {
            try
            {
                var book = new XSSFWorkbook();
                //工作簿
                var sheet = book.CreateSheet(rowName);

                //标题行
                var titlerow = sheet.CreateRow(0);
                //标题单元格
                var titlecontent = titlerow.CreateCell(0);
                //给单元格加标题内容
                titlecontent.SetCellValue(titleName);
                //创建一个样式
                var titlestyle = book.CreateCellStyle();
                //居中对齐
                titlestyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
                //设置字体
                var titlefont = book.CreateFont();
                //字体大小
                titlefont.FontHeight = 20 * 20;
                titlefont.FontName = "微软雅黑";
                //设置字体样式
                titlestyle.SetFont(titlefont);
                //设置标题样式
                titlecontent.CellStyle = titlestyle;

                Type type = typeof(T);
                //获取所有属性
                var EntityProps= type.GetProperties();
                //转成字符串
                var EntityInfo=string.Join(",", EntityProps.Select(x => x.Name));
                //分割成数组
                var EntityArray=EntityInfo.Split(",");
                //属性名称行
                var row = sheet.CreateRow(1);
                int i = 0;
                foreach (var Entity in EntityArray)
                {
                    row.CreateCell(i).SetCellValue(Entity);
                    i++;
                }

                //合并单元格
                sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, i-1));

                // 获取数据
                var list = baseRepository.GetAll().ToList();

                // 获取属性信息
                var properties = typeof(T).GetProperties();

                int j = 2;
                foreach (var item in list) // 遍历数据列表
                {
                    var dataRow = sheet.CreateRow(j++); // 创建新行

                    for (int k = 0; k < EntityArray.Length; k++) // 遍历属性名
                    {
                        string propertyName = EntityArray[k];
                        var property = properties.FirstOrDefault(p => p.Name == propertyName); // 找到对应的属性信息

                        if (property != null)
                        {
                            var value = property.GetValue(item); // 获取属性值

                            // 将属性值转换为字符串赋值给单元格
                            dataRow.CreateCell(k).SetCellValue(value?.ToString());
                        }
                        else
                        {
                             // 没有属性，给空值
                             dataRow.CreateCell(k).SetCellValue("");
                        }
                    }
                }


                byte[] bytes = null;
                using (var stream = new System.IO.MemoryStream())
                {
                    book.Write(stream);
                    return stream.ToArray();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
